﻿using System;
using System.Collections.Generic;
using System.Xml;
using System.IO;

namespace VidoopSecure
{
    public class VoiceOtp
    {
        /// <summary>
        /// Identifier of a Voice One Time Password object.
        /// </summary>
        public string Id
        {
            get { return _id; }
            set { _id = value; }
        }

        /// <summary>
        /// Phone number to which the password call is made.
        /// </summary>
        public string Phone
        {
            get { return _phone; }
            set { _phone = value; }
        }

        /// <summary>
        /// The status of the password call.
        /// </summary>
        public string SendStatus 
        {
            get { return _sendStatus; }
            set { _sendStatus = value; }
        }

        /// <summary>
        /// The status of the authentication of the Voice One Time Password.
        /// </summary>
        public string AuthenticationStatus 
        {
            get { return _authenticationStatus; }
            set { _authenticationStatus = value; }
        }
        
        /// <summary>
        /// Generation of a <see cref="VidoopSecure.VoiceOtp" /> from
        /// <paramref name="xml"/>.
        /// </summary>
        /// <param name="xml">The XML representation of a
        /// <see cref="VidoopSecure.VoiceOtp" />.</param>
        /// <returns>A <see cref="VidoopSecure.VoiceOtp" /> instance.
        /// </returns>
        static public VoiceOtp FromXml(string xml)
        {
            XmlDocument doc = new XmlDocument();
            doc.LoadXml(xml);
            return GenerateVoiceOtpFromXmlDocument(doc);
        }

        /// <summary>
        /// Generation of a <see cref="VidoopSecure.VoiceOtp" /> from
        /// <paramref name="stream"/>.
        /// </summary>
        /// <param name="stream">A <see cref="System.IO.Stream" />
        /// representing the XML of a <see cref="VidoopSecure.VoiceOtp" />.
        /// </param>
        /// <returns>A <see cref="VidoopSecure.VoiceOtp" /> instance.
        /// </returns>
        static public VoiceOtp FromXml(Stream stream)
        {
            XmlDocument doc = new XmlDocument();
            doc.Load(stream);
            return GenerateVoiceOtpFromXmlDocument(doc);
        }


        static private VoiceOtp GenerateVoiceOtpFromXmlDocument(XmlDocument doc)
        {
            VoiceOtp voiceOtp = new VoiceOtp();

            voiceOtp.Id = doc.GetElementsByTagName("id")[0].InnerText;
            voiceOtp.Phone = doc.GetElementsByTagName("phone")[0].InnerText;
            voiceOtp.SendStatus = doc.GetElementsByTagName(
                "send_status")[0].InnerText;
            voiceOtp.AuthenticationStatus = doc.GetElementsByTagName(
                "authentication_status")[0].InnerText;
            return voiceOtp;
        }

        private string _id;
        private string _phone;
        private string _sendStatus;
        private string _authenticationStatus;
    }
}
